import com.spring.demo.Student;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

public class Test_demo1 {
    public static void main(String[] args) {
        //1.得到spring
        //执行ApplicationContext后，会把xml中所以的对象初始化存储到spring容器中（饿汉模式）
        //比较费内存，一次性加载，之后读取很快
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");

        //2.从spring容器获取bean对象
        //1）第一种，通过id
//        Student student = (Student)context.getBean("student");
        //2）第二种，通过类型
        //缺点：如果在spring中一个类型储存了多个实例，会报不存在唯一定义bean异常
//        Student student = context.getBean(Student.class);
        //3）第三种，根据名称+类型
        Student student = context.getBean("student",Student.class);
        //3.使用bean
        student.hello();
    }

    public static void main1(String[] args) {
        //1.得到spring
        //beanfactory，只有在执行getBean时，才会初始化指定的对象，不加载其余对象。（软加载）
        //省内存，但效率比较低
        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
        //2.获取bean
        Student student = (Student)beanFactory.getBean("student");
        student.hello();
    }

}
